/*
 * 创建日期 2010-4-16
 *
 * 成都天和软件公司
 * 电话：028-85425861
 * 传真：028-85425861-8008
 * 邮编：610041 
 * 版权所有
 */
package  org.fantasy.cpp.core.html.edit;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import  org.fantasy.cpp.core.bean.ParamContext;
import  org.fantasy.cpp.core.html.EditHtml;
import  org.fantasy.cpp.core.html.QueryHtml;
import  org.fantasy.cpp.core.pojo.QueryItem;
import  org.fantasy.cpp.core.service.QueryService;
import  org.fantasy.cpp.core.util.ConvertUtil;
import  org.fantasy.cpp.core.util.ParseUtil;
import  org.fantasy.common.db.QryFactory;
import  org.fantasy.common.db.bean.Row;
import  org.fantasy.common.util.RequestUtil;
import  org.fantasy.common.util.StringUtil;
import  org.fantasy.common.util.WebUtil;

/**
 *  抽象条件构造器
 * @author: 王文成
 * @version: 1.0
 * @since 2010-4-16
 */
public abstract class AbstractEditHtml implements EditHtml {

	/**
	 * itemID
	 */
	protected String itemId;

	/**
	 * 控件ID
	 */
	protected String name;

	/**
	 * 显示名称
	 */
	protected String label;

	/**
	 * 设置值
	 */
	protected String value;

	/**
	 * 条件必选
	 */
	protected boolean checked;

	/**
	 * 默认是否选中
	 */
	protected boolean defChecked;

	/**
	 * 默认值
	 */
	protected String defValue;

	/**
	 * 类型名称
	 */
	protected String className;

	/**
	 * 类型名称
	 */
	protected String cssClass;

	/**
	 * 样式定义
	 */
	protected String style;

	/**
	 * 格式定义
	 */
	protected String format;

	/**
	 * 数据源
	 */
	protected String source;

	/**
	 * 数据源
	 */
	protected ParamContext param;

	/**
	 * 当前请求
	 */
	protected HttpServletRequest request;

	/**
	 * 当前请求类型
	 */
	protected String itemType;

	/**
	 * 是否只读
	 */
	protected boolean readonly;

	/**
	 * 验证
	 */
	protected Validate validate;

	/**
	 * 是否多选
	 */
	protected String multiple;

	/**
	 * 是否是异步
	 */
	protected boolean async;

	/**
	 * ContextPath
	 */
	protected String ctx;

	/**
	 * 初始ID
	 */
	protected String startId;

	/**
	 * 操作符(新增、修改、删除)
	 */
	protected String opt;

	/**
	 * 对象ID
	 */
	protected String id;

	public AbstractEditHtml(HttpServletRequest request, ParamContext param) {
		this.param = param;
		this.ctx = request.getContextPath();
		this.itemId = param.get(ITEM_ID);
		this.name = param.get(NAME);
		this.label = param.get(LABEL);
		this.value = param.get(VALUE);
		this.style = param.get(CSS_STYLE);
		this.className = param.get(CLASS_NAME);
		this.defValue = param.get(DEF_VALUE);
		this.cssClass = param.get(CSS_CLASS);
		this.format = param.get(FORMAT);
		this.validate = new Validate(param.get(VALIDATE));
		this.source = param.get(SOURCE);
		this.request = request;
		this.readonly = param.isTrue(READONLY);
		this.multiple = param.get(MULTIPLE);
		this.async = param.isTrue(ASYNC);
		this.startId = param.get(START_ID);
		this.opt = RequestUtil.getParameter(request, "opt");
		this.id = RequestUtil.getParameter(request, "id");
	}

	/**
	 * 得到名字
	 * 
	 * @return
	 * @throws Exception
	 */
	protected String getLabel() {
		return label + "：";
	}

	/**
	 * 是否只读
	 * 
	 * @return
	 * @throws Exception
	 */
	protected String getIsReadonly(String s) {
		return readonly ? " readonly=\"true\" " : "";
	}

	/**
	 * 取得样式
	 * 
	 * @return
	 */
	protected String getStyleHtml() {
		StringBuffer html = new StringBuffer(32);
		if (StringUtil.isValid(style)) {
			html.append(" style='" + style + "'");
		}
		return html.toString();
	}

	/**
	 * 取得css样式
	 * 
	 * @return
	 */
	protected String getClassHtml() {
		StringBuffer html = new StringBuffer(32);
		if (StringUtil.isValid(cssClass)) {
			if (validate.isRequired()) {
				html.append(" class='" + cssClass + " " + NULL_CLASS + "'");
			} else {
				html.append(" class='" + cssClass + "'");
			}
		} else {
			if (validate.isRequired()) {
				html.append(" class='" + NULL_CLASS + "'");
			}
		}
		return html.toString();
	}

	/**
	 * 取得验证
	 * 
	 * @return
	 */
	protected String getValidate() {
		StringBuffer html = new StringBuffer(128);
		String val = validate.toString();
		if (StringUtil.isValid(val)) {
			html.append(" validate=\"" + val + "\"");
		} else {
			html.append(" ");
		}
		return html.toString();
	}

	/**
	 * 取得multiple
	 * 
	 * @return
	 */
	protected String getMultiple() {
		StringBuffer html = new StringBuffer(32);
		if (!"".equals(multiple)) {
			html.append(" multiple='" + multiple + "'");
		}
		return html.toString();
	}

	/**
	 * 取得数据源
	 * 
	 * @return
	 */
	protected Object getSource() throws Exception {
		String config = getConfig(source);
		QueryService queryService = (QueryService)WebUtil.getBean(request, "queryService");
		if (source.startsWith("id:")) {
			Long queryId = new Long(config);
			return queryService.qryList(queryId, new HashMap<String,Object>());
		} if (source.startsWith("cache:")) {
			
		} else if (source.startsWith("sql:")) {
			QryFactory qryFactory = (QryFactory) WebUtil.getBean(request, "qryFactory");
			return qryFactory.getQuery(config).list();
		} else if (source.startsWith("json:")) {
			return ParseUtil.json2List(config);
		}
		return new ArrayList<Row>(0);
	}

	private String getConfig(String source) {
		int start = source.indexOf(":") + 1;
		return source.substring(start, source.length());
	}

	/**
	 * 取得通用查询Map
	 * 
	 * @param item
	 * @return
	 * @throws Exception
	 */
	private Map<String, Object> getQueryMap(QueryItem item) throws Exception {
		Map<String, Object> qryMap = RequestUtil.getRequestMap(request);
		String config = item.getAttr(QueryHtml.START_ID);
		String startId = ConvertUtil.covert(request, config);
		qryMap.put(QueryHtml.START_ID, startId);
		return qryMap;
	}

	/**
	 * 特殊字符过滤
	 * 
	 * @return
	 */
	public String charFilter(String input) {
		StringBuilder filtered = new StringBuilder(64);
		char c;
		for (int i = 0; i < input.length(); i++) {
			c = input.charAt(i);
			if (c == '<') {
				filtered.append("&lt;");
			} else if (c == '>') {
				filtered.append("&gt;");
			} else if (c == '\"') {
				filtered.append("&quot;");
			} else if (c == '&') {
				filtered.append("&amp;");
			} else {
				filtered.append(c);
			}
		}
		return (filtered.toString());
	}

	/**
	 * 取得值
	 * 
	 * @return
	 */
	protected String getValue() {
		// OPT判断方式
		if (StringUtil.isValid(opt)) {
			if (OPT_EDIT.equals(opt)) {
				return value;
			} else if (OPT_ADD.equals(opt)){
				return defValue;
			}
			return StringUtil.tryThese(value, defValue);
		} else {//以ID判断方式
			if(StringUtil.isValid(id)){
				return value;
			}
			return defValue;
		}
	}

	/**
	 * 取得组件ID
	 * 
	 * @return
	 */
	protected String getStartId() {
		return name + "Combo";
	}

	/**
	 * 取得组件ID
	 * 
	 * @return
	 */
	protected String getComboId() {
		return name + "Combo";
	}
}
